class Solution {
public:
    int minFallingPathSum(vector<vector<int>>& matrix)
    {
        int n = matrix.size(), m = matrix[0].size();
        vector<vector<int>> dp(n, vector<int>(m + 2, INT_MAX));
        for (int i = 1; i < m + 1; i++) dp[0][i] = matrix[0][i - 1];
        for (int i = 1; i < n; i++)
        {
            for (int j = 1; j < m + 1; j++)
            {
                dp[i][j] = min({ dp[i - 1][j - 1], dp[i - 1][j], dp[i - 1][j + 1] }) + matrix[i][j - 1];
            }
        }
        return *min_element(dp[n - 1].begin(), dp[n - 1].end());
    }
};